/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package similarite;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.List;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Clement
 */
public class SommeVecteur extends Thread {

    private File file;
    private Scanner scan;
    private Normalisation n;
    private boolean ok;
    private int nCol;

    public File getFile() {
        return file;
    }

    public void setFile(File file) {
        this.file = file;
    }

    public Normalisation getN() {
        return n;
    }

    public void setN(Normalisation n) {
        this.n = n;
    }

    public boolean isOk() {
        return ok;
    }

    public void setOk(boolean ok) {
        this.ok = ok;
    }

    public int getnCol() {
        return nCol;
    }

    public void setnCol(int nCol) {
        this.nCol = nCol;
    }

    public SommeVecteur(File file) {
        ok = false;
        this.file = file;
        try {
            List<Integer> l;

            this.scan = new Scanner(file);

            this.n = new Normalisation(file, this);
            this.n.initImage();
            this.nCol = 128;
        } catch (FileNotFoundException ex) {
            Logger.getLogger(SommeVecteur.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public SommeVecteur(String s) {
        this.scan = new Scanner(s);
        System.out.println(s);
        this.n = new Normalisation(s, this);
        this.n.initFile();
    }

    public SommeVecteur(String s, int i) {
        this.scan = new Scanner(s);
        System.out.println(s);
        this.n = new Normalisation(s, this);
        this.n.initFile();
        nCol = i;
    }

    @Override
    synchronized public void run() {
        if (file != null) {
            scan.next(); //max col
            scan.next(); //max ligne 
        }

        if (nCol == 128) {

            while (scan.hasNext()) {
                int s = 0;
                if (file != null) {
                    for (int j = 0; j < 5; j++) {
                        scan.next();
                    }
                }
                if (nCol == 128) {
                    for (int j = 0; j < nCol; j++) {
                        s += Float.parseFloat(scan.next());
                    }
                }

//                System.err.println("Norme : "+s);
                this.n.setNorme(s);
                this.n.run();
            }
        } else {
            int s = 0;
            while (scan.hasNext()) {
                for (int j = 0; j < nCol; j++) {
                    Float f = Float.parseFloat(scan.next());
                    if (s > f && f != 0 || s == 0) {
                        s = 0;
                        s += f;
                    }
                }
            }
            this.n.setNorme(s);
//            for (int x = 0; x < nCol; x++) {

                this.n.run();
//            }
        }

        this.n.endFile();
        scan.close();
        ok = true;
    }

    synchronized public void getNewNorme() {
        this.notify();
    }
}